package theOrbalEra.common;


import theOrbalEra.client.handler.GuiHandler;
import theOrbalEra.common.block.BlockOrbalStreetLight;
import theOrbalEra.common.block.OrbalBlockQuatzExchange;
import theOrbalEra.common.block.BlockOrbalFactory;
import theOrbalEra.common.block.TileEntityOrballFactory;
import theOrbalEra.common.block.TileEntityStreetLight;
import theOrbalEra.common.item.OrbalItemEleBag;
import theOrbalEra.common.item.OrbalItemElement;
import theOrbalEra.common.item.OrbalItemQuartz;
import theOrbalEra.common.item.OrbalItemOrbment;
import theOrbalEra.common.network.PacketHandler;
import theOrbalEra.common.network.PacketPlayerDataSave;
import theOrbalEra.common.util.OrbalUtils;
import theOrbalEra.common.util.CreativeTabTheOrbalEra;
import theOrbalEra.common.util.Element;
import theOrbalEra.common.util.Localization;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.ICraftingHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;

@NetworkMod(serverSideRequired = false, clientSideRequired = true,packetHandler=PacketHandler.class,channels = { "TheOrbalEra" })
@Mod(modid = "TheOrbalEra", name = "TheOrbalEra", version = "0.0.1")
public class TheOrbalEra implements ICraftingHandler{
    //item
	public static Item orbment;
	public static  int orbmentId;
	public static Item fireElement;
	public static int fireElementId;
	public static Item waterElement;
	public static int waterElementId;
	public static Item windElement;
	public static int windElementId;
	public static Item earthElement;
	public static int earthElementId;
	public static Item timeElement;
	public static int timeElementId;
	public static Item spaceElement;
	public static int spaceElementId;
	public static Item unrealElement;
	public static int unrealElementId;
	public static Item quartz;
	public static int quartzId;
	public static Item ele_bag;
	public static int ele_bagId;
    //block
	public static Block quatzExchange;
	public static Block orbalFactory;
	public static int quatzExchangeId;
	public static int orbalFactoryId;
	public static Block streetLight;
	public static int streetLightId;

    //other
	//public static boolean modOff = false;
	// Network
	public PacketPlayerDataSave playerData = new PacketPlayerDataSave();
	
    //CreativeTabs 
	public static CreativeTabs theOrbalEra	= new CreativeTabTheOrbalEra("The Orbal Era");
	
	@Mod.Instance("TheOrbalEra")
	public static TheOrbalEra instance;

	@SidedProxy(clientSide = "theOrbalEra.client.ClientProxy", serverSide = "theOrbalEra.common.CommonProxy")
	public static CommonProxy proxy;
	
	@Mod.PreInit
	public void preLoad(FMLPreInitializationEvent event) {
		final Configuration config = new Configuration(event.getSuggestedConfigurationFile());
		config.load();
		orbmentId = config.getItem("orbment", 10000).getInt();
		quatzExchangeId = config.getBlock("quatzExchange", 225).getInt();
		orbalFactoryId = config.getBlock("orbalFactory", 226).getInt();
		streetLightId=config.getBlock("streetLighr", 227).getInt();
		fireElementId = config.getItem("fireElement", 10001).getInt();
		waterElementId = config.getItem("waterElement", 10002).getInt();
		earthElementId = config.getItem("earthElement", 10003).getInt();
		windElementId = config.getItem("windElement", 10004).getInt();
		timeElementId = config.getItem("timeElement", 10005).getInt();
		spaceElementId = config.getItem("spaceElement", 10006).getInt();
		unrealElementId = config.getItem("unrealElement", 10007).getInt();
		quartzId = config.getItem("quartz", 10009).getInt();
		ele_bagId = config.getItem("ele_bag", 10008).getInt();
		config.save();
		

	}

	@Mod.Init
	public void onLoad(FMLInitializationEvent event) {
		//Defined items
		orbment = new OrbalItemOrbment(orbmentId);
		fireElement = new OrbalItemElement(fireElementId, Element.Fire).setUnlocalizedName("fireElement");
		waterElement = new OrbalItemElement(waterElementId, Element.Water).setUnlocalizedName("waterElement");
		earthElement = new OrbalItemElement(earthElementId, Element.Earth).setUnlocalizedName("earthElement");
		windElement = new OrbalItemElement(windElementId, Element.Wind).setUnlocalizedName("windElement");
		timeElement = new OrbalItemElement(timeElementId, Element.Time).setUnlocalizedName("timeElement");
		spaceElement = new OrbalItemElement(spaceElementId, Element.Space).setUnlocalizedName("spaceElement");
		unrealElement = new OrbalItemElement(unrealElementId, Element.Unreal).setUnlocalizedName("unrealElement");
		quartz = new OrbalItemQuartz(quartzId).setUnlocalizedName("quartz");
		ele_bag = new OrbalItemEleBag(ele_bagId).setUnlocalizedName("ele_bag");
       //Defined blocks
		quatzExchange = new OrbalBlockQuatzExchange(quatzExchangeId).setUnlocalizedName("quatzExchange");
		orbalFactory = new BlockOrbalFactory(orbalFactoryId).setUnlocalizedName("orbalFactory");
		streetLight=new BlockOrbalStreetLight(streetLightId, Material.rock).setUnlocalizedName("streetLight");
		//registry blocks
		proxy.registerBlock(quatzExchange);
		proxy.registerBlock(orbalFactory);
        proxy.registerBlock(streetLight);
		//registry items
		proxy.registerItem(orbment);
		proxy.registerItem(fireElement);
		proxy.registerItem(waterElement);
		proxy.registerItem(windElement);
		proxy.registerItem(earthElement);
		proxy.registerItem(timeElement);
		proxy.registerItem(spaceElement);
		proxy.registerItem(unrealElement);
		proxy.registerItem(quartz);
		proxy.registerItem(ele_bag);
		//registry tileentity
		GameRegistry.registerTileEntity(TileEntityOrballFactory.class,"TileEntityGloriousFactory");
		GameRegistry.registerTileEntity(TileEntityStreetLight.class,"TileEntityStreetLight");
		
        // event
		MinecraftForge.EVENT_BUS.register(new OrbalEventHandler());
		//register guiHandler
		NetworkRegistry.instance().registerGuiHandler(this, new GuiHandler());
		//register packetHandler,connectionHandler
		NetworkRegistry.instance().registerConnectionHandler(playerData);
		//add name
		OrbalUtils.addName(quatzExchange, "Quatz Exchange","回路兑换台");
		OrbalUtils.addName(orbalFactory, "Orbal Factory","曜石加工");
		OrbalUtils.addName(orbment, "Orbment","导力器");
		OrbalUtils.addName(fireElement, "Fire Element","火曜石碎片");
		OrbalUtils.addName(waterElement, "Water Element","水曜石碎片");
		OrbalUtils.addName(windElement, "Wind Element","风曜石碎片");
		OrbalUtils.addName(earthElement, "Earth Element","土曜石碎片");
		OrbalUtils.addName(timeElement, "Time Element","时曜石碎片");
		OrbalUtils.addName(spaceElement, "Space Element","空曜石碎片");
		OrbalUtils.addName(unrealElement, "Unreal Element","幻曜石碎片");
		OrbalUtils.addName(quartz, "Quartz","回路");
		OrbalUtils.addName(ele_bag, "Element Bag","曜石包");
		Localization.addLocalization("/mods/theOrbalEra/lang/", "en_US");
		//proxy load
//		  EntityRegistry.registerModEntity(OrbalEntityMagicEffectDrakLance.class, "OrbalEntityMagicEffectDrakLance", 0, this, 250, 5, false);
		  
		RegistryRecipes();
		GameRegistry.registerCraftingHandler(this);
		
		proxy.onLoad();
		proxy.registerRenderInformation();
	}
	
	private void RegistryRecipes() {
		GameRegistry.addRecipe(new ItemStack(ele_bag,1), new Object[] {
			"# #",
			"#X#",
			"###",
			Character.valueOf('#'),new ItemStack(Block.cloth,1),
			Character.valueOf('X'),new ItemStack(fireElement,1)
			});
		GameRegistry.addRecipe(new ItemStack(ele_bag,1), new Object[] {
			"# #",
			"#X#",
			"###",
			Character.valueOf('#'),new ItemStack(Block.cloth,1),
			Character.valueOf('X'),new ItemStack(waterElement,1)
			});
		GameRegistry.addRecipe(new ItemStack(ele_bag,1), new Object[] {
			"# #",
			"#X#",
			"###",
			Character.valueOf('#'),new ItemStack(Block.cloth,1),
			Character.valueOf('X'),new ItemStack(earthElement,1)
			});
		GameRegistry.addRecipe(new ItemStack(ele_bag,1), new Object[] {
			"# #",
			"#X#",
			"###",
			Character.valueOf('#'),new ItemStack(Block.cloth,1),
			Character.valueOf('X'),new ItemStack(windElement,1)
			});
		GameRegistry.addRecipe(new ItemStack(ele_bag,1), new Object[] {
			"# #",
			"#X#",
			"###",
			Character.valueOf('#'),new ItemStack(Block.cloth,1),
			Character.valueOf('X'),new ItemStack(timeElement,1)
			});
		GameRegistry.addRecipe(new ItemStack(ele_bag,1), new Object[] {
			"# #",
			"#X#",
			"###",
			Character.valueOf('#'),new ItemStack(Block.cloth,1),
			Character.valueOf('X'),new ItemStack(spaceElement,1)
			});
		GameRegistry.addRecipe(new ItemStack(ele_bag,1), new Object[] {
			"# #",
			"#X#",
			"###",
			Character.valueOf('#'),new ItemStack(Block.cloth,1),
			Character.valueOf('X'),new ItemStack(unrealElement,1)
			});
		
	}

	/**
	 * Called when 1 game loop is done.
	 * @param minecraft
	 */
	public void onTickInGame() {
			proxy.onTickInGame();
		}

	@Override
	public void onCrafting(EntityPlayer player, ItemStack item,IInventory craftMatrix) {
//		for(int i=0;i<9;i++)
//			System.out.println(craftMatrix.getStackInSlot(i).getDisplayName());
		
	}

	@Override
	public void onSmelting(EntityPlayer player, ItemStack item) {
	}
	

}